<template>
  <div class="header">
    <!-- 顶部快捷导航栏 -->
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="javascript:;">小米商城</a>
          <a href="javascript:;">MUI</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">协议规则</a>
        </div>
        <div class="topbar-user">
          <a href="javascript:;" v-if="username">{{ username }}</a>
           <a href="javascript:;" v-if="username" @click="logout">退出</a>
          <a href="javascript:;" v-else @click="login()">登录</a>
          <a href="javascript:;" v-if="username">我的订单</a>
          <a href="/#/regiest" v-else>注册</a>
          <a target="_blank" @click="goToCart()" class="shop-cart"
            ><span class="icon-cart"></span> 购物车
            {{ this.$store.state.cartCount }}</a
          >
          <!-- <a href="javascript:;"  @click="test()">测试</a> -->
        </div>
      </div>
    </div>
    <!-- nav导航栏 -->
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/#/index"></a>
        </div>
        <div class="header-menu">
          <div class="menu-item">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li v-for="(item, index) in phoneList" :key="index">
                  <a :href="'/#/product/' + item.id" target="_blank">
                    <div class="pro-img">
                      <img :src="item.mainImage" :alt="item.subtitle" />
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price | currency }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="menu-item">
            <span>RedMi红米</span>
            <div class="children">
              <ul>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi1.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">Redmi K30S 至尊纪念版</div>
                    <div class="pro-price">¥2599.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi2.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">Redmi K30 至尊纪念版</div>
                    <div class="pro-price">¥2499.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi3.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">Redmi K30 Pro 系列</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi4.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">RedMi 10X</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi2.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">RedMi Note8</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="../../public/imgs/redmi-img/redmi1.jpg"
                        alt=""
                      />
                    </div>
                    <div class="pro-name">Redmi K30系列</div>
                    <div class="pro-price">¥2399.00元</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="menu-item">
            <span>电视</span>
            <div class="children">
              <ul>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-1.jpg" alt="" />
                    </div>
                    <div class="pro-name">小米电视大师 82英寸至尊纪念版</div>
                    <div class="pro-price">¥49999.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-2.jpg" alt="" />
                    </div>
                    <div class="pro-name">小米电视大师 82英寸</div>
                    <div class="pro-price">¥9999.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-3.png" alt="" />
                    </div>
                    <div class="pro-name">小米透明电视</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-4.jpg" alt="" />
                    </div>
                    <div class="pro-name">小米电视 大师 65英寸OLED</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-5.jpg" alt="" />
                    </div>
                    <div class="pro-name">小米电视4A 60英寸</div>
                    <div class="pro-price">¥2399.00元</div>
                  </a>
                </li>
                <li>
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img src="../../public/imgs/nav-img/nav-3-6.png" alt="" />
                    </div>
                    <div class="pro-name">小米电视大师 82英寸</div>
                    <div class="pro-price">¥1799.00元</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="menu-item">
            <span>笔记本</span>
          </div>
          <div class="menu-item">
            <span>家电</span>
          </div>
          <div class="menu-item">
            <span>路由器</span>
          </div>
          <div class="menu-item">
            <span>服务</span>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" />
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'nav-header',
  data () {
    return {
      phoneList: []
      // username: ''
    }
  },
  created () {
    this.getPhoneList()
  },
  computed: {
    username () {
      return this.$store.state.username
    },
    cartCount () {
      return this.$store.state.cartCount
    }
  },
  methods: {
    // 获取产品数据
    getPhoneList () {
      this.$http
        .get('http://mi.futurefe.com/api/products', {
          params: {
            categoryId: '100012',
            pageSize: 6
          }
        })
        .then((res) => {
          this.phoneList = res.list.slice(0, 6)
          // console.log(this.phoneList)
        })
    },
    // 跳转到购物车页面
    goToCart () {
      this.$router.push('/cart')
    },
    // 跳转到登录页面
    login () {
      this.$router.push('/login')
    },
    logout () {
      this.$http.post('/api/user/logout').then(() => {
        alert('退出成功')
        this.$cookie.set('userId', '', { expires: '-1' })
        this.$store.dispatch('saveUserName', '')
        this.$store.dispatch('saveCartCount', '0')
      })
    }
  },
  filters: {
    currency (val) {
      if (!val) return '0.00'
      return '¥ ' + val.toFixed(2) + '元'
    }
  }
}
</script>

<style lang="scss">
@import '../assets/scss/mixin.scss';
@import '../assets/scss/config.scss';
.header {
  .nav-topbar {
    height: 39px;
    line-height: 39px;
    background-color: $colorB;
    color: #b0b0b0;
    .container {
      @include flex();

      a {
        color: #b0b0b0;
        display: inline-block;
        margin-right: 17px;
      }
      .shop-cart {
        width: 110px;
        background-color: $colorA;
        color: #fff;
        text-align: center;
        .icon-cart {
          @include bgImg(16px, 12px, '../../public/imgs/icon-cart-checked.png');
          margin-right: 4px;
        }
      }
    }
  }
  .nav-header {
    background-color: #fff;
    .container {
      position: relative;
      height: 112px;
      @include flex();
      .header-logo {
        width: 55px;
        height: 55px;
        background-color: $colorA;
        a {
          display: block;
          width: 110px;
          height: 55px;
          // margin-left: -55px;
          &:before {
            content: '';
            @include bgImg(55px, 55px, '/imgs/mi-logo.png');
          }
          &:after {
            content: '';
            @include bgImg(55px, 55px, '/imgs/mi-home.png');
          }
          &:hover {
            margin-left: -55px;
          }
          transition: margin 0.3s;
        }
      }
      .header-menu {
        display: flex;
        margin-right: 164px;
        .menu-item {
          &:hover .children {
            opacity: 1;
            height: 220px;
          }
          span {
            font-size: 16px;
            color: $colorB;
            font-weight: 700;
            margin-right: 25px;
            cursor: pointer;
            &:hover {
              color: $colorA;
            }
          }
          .children {
            overflow: hidden;
            position: absolute;
            top: 112px;
            left: calc((1226px - 100vw) / 2);
            // width: 1226px;
            width: 100vw;
            // opacity: 1;
            // height: 220px;
            opacity: 0;
            height: 0;
            box-shadow: 6px 6px 6px 0px rgba(0, 0, 0, 0.11);
            transition: height 0.3s;
            z-index: 99;
            background-color: #fff;

            ul {
              margin-top: 26px;
              text-align: center;
              width: 1226px;
              margin: auto;
              li {
                position: relative;
                width: 16.6%;
                float: left;
                a {
                  display: inline-block;
                  .pro-img {
                    height: 111px;
                    img {
                      width: auto;
                      height: 111px;
                    }
                  }
                  .pro-name {
                    margin-top: 18px;
                    font-size: 12px;
                    font-weight: 700;
                    color: #333;
                  }
                  .pro-price {
                    margin-top: 4px;
                    font-size: 12px;
                    font-weight: 700;
                    color: #ff6600;
                  }
                }
                &:not(:last-child):after {
                  position: absolute;
                  top: 0px;
                  right: 0;
                  content: '';
                  width: 1px;
                  height: 100px;
                  background-color: #d7d7d6;
                }
              }
            }
          }
        }
      }
      .header-search {
        .wrapper {
          width: 319px;
          height: 50px;
          border: 1px solid #e0e0e0;
          display: flex;
          align-items: center;
          input {
            width: 264px;
            height: 50px;
            border: none;
            border-right: 1px solid #e0e0e0;
            padding-left: 14px;
            box-sizing: border-box;
          }
          a {
            @include bgImg(18px, 18px, '../../public/imgs/icon-search.png');
            margin-left: 17px;
          }
        }
      }
    }
  }
}
</style>
